package com.zcxy.fast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.entity.SysDict;
import com.zcxy.fast.entity.SysDictItem;
import com.zcxy.fast.mapper.SysDictItemMapper;
import com.zcxy.fast.mapper.SysDictMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.mybatisExtend.LambdaValiteCondition;
import com.zcxy.fast.service.ISysDictItemService;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.vo.SysDictItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;



/**
 * 平台-字典值业务层处理
 *
 * @author zcc
 * @email  860474958@qq.com
 * @date   2020-12-18 15:15:57
 */
@Service
@Slf4j
public class SysDictItemServiceImpl extends BaseServiceImpl<SysDictItemMapper, SysDictItem> implements ISysDictItemService {

    @Autowired
   private  SysDictMapper sysDictMapper;

    @Override
    public IPage<SysDictItemVo> queryPageList(PageQuery<SysDictItem> queryVo) {
        SysDictItem sysDictItem=queryVo.getT() == null ? new SysDictItem() : queryVo.getT();
        QueryWrapper queryWrapper = new QueryWrapper(sysDictItem);
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<SysDictItem> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopyUtil.copyPage(sysUserVoIPage, SysDictItemVo.class);
    }

    @Override
    public List<SysDictItemVo> queryList(SysDictItem sysDictItem) {
        SysDictItem sysDictItemq=sysDictItem == null ? new SysDictItem() : sysDictItem;
        QueryWrapper queryWrapper = new QueryWrapper(sysDictItemq);
        queryWrapper.orderByAsc("sort_no");
        List<SysDictItemVo> SysDictItemVos= BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper),SysDictItemVo.class);
        return SysDictItemVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysDictItem(SysDictItem sysDictItem) {
        LambdaValiteCondition<SysDictItem> valiteCondition = LambdaValiteCondition.newInstance(sysDictItem);
        valiteCondition.addField(SysDictItem::getName,SysDictItem::getDictId).addErrInfo("名称不能重复");
        validFiledExist(valiteCondition);
        valiteCondition.clear();
        valiteCondition.addField(SysDictItem::getCode,SysDictItem::getDictId).addErrInfo("编码不能重复");
        validFiledExist(valiteCondition);
        SysDict sysDict=sysDictMapper.selectById(sysDictItem.getDictId());
        sysDictItem.setDictCode(sysDict.getCode());
        baseMapper.insert(sysDictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysDictItem(SysDictItem sysDictItem) {
        LambdaValiteCondition<SysDictItem> valiteCondition = LambdaValiteCondition.newInstance(sysDictItem);
        valiteCondition.addField(SysDictItem::getName,SysDictItem::getDictId).addErrInfo("名称不能重复");
        valiteCondition.addExcludes(SysDictItem::getId);
        validFiledExist(valiteCondition);
        valiteCondition.clear();
        valiteCondition.addExcludes(SysDictItem::getId);
        valiteCondition.addField(SysDictItem::getCode,SysDictItem::getDictId).addErrInfo("编码不能重复");
        validFiledExist(valiteCondition);
        baseMapper.updateById(sysDictItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysDictItem(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }



}
